package com.backstage.controller;

import com.backstage.common.annotation.log.Operate;
import com.backstage.common.base.bean.Message;
import com.backstage.common.page.Page;
import com.backstage.common.uitil.JsonUtil;
import com.backstage.entity.Menu;
import com.backstage.service.MenuService;
import com.fasterxml.jackson.core.JsonProcessingException;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @Project authority
 * @Package com.backstage.controller
 * @Data 2017/7/19下午 2:32.
 * @Aurhor 阮雪峰
 */
@RequestMapping("/menu")
@Controller
public class MenuController extends BaseController{

    @Resource(name="menuService")
    private MenuService menuService;

    /**
     *分页查询菜单
     * @param page
     * @return
     */
    @RequiresPermissions("sys:menu:list")
    @RequestMapping(value = "/queryByPage", method = RequestMethod.POST)
    @ResponseBody
    public Page<Menu> queryByPage(Page<Menu> page, Menu menu){
        menuService.menuByPage(page, menu);
        return page;
    }

    /**
     * 新增修改时查询菜单
     * @param menu
     * @return
     */
    @RequestMapping(value = "/getSuperList", method = RequestMethod.POST)
    @ResponseBody
    public List<Menu> getSuperList(Menu menu){
        return menuService.getSuperList(menu);
    }

    /**
     * 新增
     * @param menu
     * @return
     */
    @Operate("菜单管理：新增")
    @RequiresPermissions("sys:menu:add")
    @RequestMapping(value = "/add", method = RequestMethod.POST)
    @ResponseBody
    public Message add(Menu menu){
        menu.setCreator(this.getLoginUser().getUid());
        menuService.add(menu);
        return Message.ADD_SUCCESS;
    }

    /**
     * 删除
     * @param menus
     * @return
     */
    @Operate("菜单管理：删除")
    @RequiresPermissions("sys:menu:delete")
    @RequestMapping(value = "delete", method = RequestMethod.POST)
    @ResponseBody
    public Message delete(@RequestBody ArrayList<Menu> menus){
        menuService.delete(menus);
        return Message.DELETE_SUCCESS;
    }

    /**
     * 查询菜单
     * @param menu
     * @return
     */
    @RequestMapping(value = "/getMenuById", method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> getMenuById(Menu menu) throws JsonProcessingException {
        return JsonUtil.objectToMap(menuService.getMenuById(menu));
    }

    /**
     * 修改
     * @param menu
     * @return
     */
    @Operate("菜单管理：修改")
    @RequiresPermissions("sys:menu:edit")
    @RequestMapping("/edit")
    @ResponseBody
    public Message edit(Menu menu){
        menu.setEditor(this.getLoginUser().getUid());
        menuService.edit(menu);
        return Message.EDIT_SUCCESS;
    }
}
